*Input code for the results in the RCFS paper

clear
set mem 1g
use "\Bias\test\controls.dta"

sort permno yeara
merge permno yeara using "\Jeff\rating_included.dta",nokeep
tab _merge
drop _merge

keep if yeara>1984 & yeara<2006
replace ratdum=0 if ratdum==.

****************************************
*Table 1: distribution of firms by type*
****************************************

tab yeara if ratdum==1
tab yeara

gen bmpv=bveqpv/mktcapcomp
gen dd=vol*mktlev

replace year=yeara if year==. & yeara~=.

sort permno year
merge permno year using \Jeff\Data\BA.dta,nokeep
tab _merge
drop _merge

*********
*Table 2*
*********

tabstat mvalfirm bmpv sp500 mktlev vol dd if ratdum==0,stats(mean) by(year)
tabstat mvalfirm bmpv sp500 mktlev vol dd if ratdum==1,stats(mean) by(year)

*****************************************
*Saving DV and bond data
clear
use \Jeff\final_ann.dta
tsset permno year

drop Frating
gen Frating=F.rating

keep permno year rating Frating durann

sort permno year

save \Jeff\DV.dta,replace
******************************************

**********************
*Table 3: OLS results*
**********************

use "/Users/mkacperc/Jeff/final_ann.dta",clear
tsset permno year

drop Frating
gen Frating=F.rating

merge permno year using "/Users/mkacperc/Jeff/bias.dta",nokeep
tab _merge
drop _merge

gen lnbmhk=ln(bmkthk)
gen lnbmpv=ln(bmktpv)
gen dd=mktlev*vol

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/BA.dta",nokeep
tab _merge
drop _merge

gen nba2=nba
replace nba2=0 if nba2==. & noan~=.

gen sum2=Frating+noan+lnassets+lnbmpv+retann+sp500+mktlev+vol+dd+durann+Bias21+Rec21+tang+cash

**********************************
*Table 3A (Summary stats for OLS)*
**********************************

tabstat Frating noan lnassets lnbmpv retann sp500 mktlev vol dd tang cash nba if sum2~=.,stats(mean median sd)
tabstat Frating noan lnassets lnbmpv retann sp500 mktlev vol dd tang nba durann Bias21 Rec21 if sum2~=.,stats(mean median sd)

*Demeaning the control variables
egen meannoan=mean(noan) if sum2~=.
egen meanbias=mean(Bias21) if sum2~=.
egen meanacc=mean(Acc21) if sum2~=.
egen meanass=mean(lnassets) if sum2~=.
egen meanbm=mean(lnbmpv) if sum2~=.
egen meanret=mean(retann) if sum2~=.
egen meanlev=mean(mktlev) if sum2~=.
egen meantang=mean(tang) if sum2~=.
egen meandd=mean(dd) if sum2~=.
egen meanvol=mean(vol) if sum2~=.
egen meansp500=mean(sp500) if sum2~=.
egen meancash=mean(cash) if sum2~=.

replace noan=noan-meannoan
replace lnassets=lnassets-meanass
replace lnbmpv=lnbmpv-meanbm
replace retann=retann-meanret
replace mktlev=mktlev-meanlev
replace tang=tang-meantang
replace dd=dd-meandd
replace vol=vol-meanvol
replace sp500=sp500-meansp500
replace cash=cash-meancash

gen bias=Bias21-meanbias
gen acc=Acc21-meanacc

gen sic2=int(sic3/10)

****************************
*Table 3B (OLS Regressions)*
****************************

*OLS Specification

xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3.xls, replace se bdec(3)
xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3.xls, append se bdec(3)
xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3.xls, append se bdec(3)
xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3.xls, append se bdec(3)

*Alternative regression with bias
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias.xls, replace se bdec(3)
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias.xls, append se bdec(3)
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias.xls, append se bdec(3)
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias.xls, append se bdec(3)

*Analysis of partial R2 (with and without analyst forecast)
winsor roe,gen(wroe3) p(0.025)

xi: cluster2 Frating noan if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating noan wroe3 if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating dd if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating dd wroe3 if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating lnassets mktlev tang vol cash if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating wroe3 lnassets mktlev tang vol cash if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating noan wroe3 lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating noan lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
xi: cluster2 Frating noan wroe3 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)


*Alternative regression with bias*coverage
gen inter=noan*bias

xi: cluster2 Frating bias noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_inter.xls, replace se bdec(3)
xi: cluster2 Frating bias noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_inter.xls, append se bdec(3)
xi: cluster2 Frating bias noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_inter.xls, append se bdec(3)
xi: cluster2 Frating bias noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_inter.xls, append se bdec(3)

*Conditioning on bond coverage
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=. & nba2<=1, fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias_bondcov.xls, replace se bdec(3)
xi: cluster2 Frating bias lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=. & nba2>=2 & nba2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_bias_bondcov.xls, append se bdec(3)

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/bondyields.dta",nokeep
tab _merge
drop _merge

gen Fyield=Fannyield
*replace Fyield=Fvwyield if Fyield==. & Fvwyield~=.

winsor Fyield,gen(WFyield) p(0.01)
winsor Fyield,gen(WFyield2) p(0.025)

winsor annyield,gen(Wyield) p(0.01)
winsor annyield,gen(Wyield2) p(0.025)
winsor annyield,gen(Wyield3) p(0.005)

egen meanrating=mean(rating) if sum2~=.
replace rating=rating-meanrating

gen inter=rating*noan
gen inter2=rating*bias

xi: cluster2 Fyield rating lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield.xls, replace se bdec(3)
xi: cluster2 Fyield rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield.xls, append se bdec(3)
xi: cluster2 Fyield rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield.xls, append se bdec(3)
xi: cluster2 Fyield rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield.xls, append se bdec(3)

*Regression with coverage interaction
xi: cluster2 Fyield rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2.xls, replace se bdec(3)
xi: cluster2 Fyield rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2.xls, append se bdec(3)
xi: cluster2 Fyield rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2.xls, append se bdec(3)
xi: cluster2 Fyield rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2.xls, append se bdec(3)

*Regression with bias interaction
xi: cluster2 Fyield rating bias inter2 lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2_bias.xls, replace se bdec(3)
xi: cluster2 Fyield rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2_bias.xls, append se bdec(3)
xi: cluster2 Fyield rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2_bias.xls, append se bdec(3)
xi: cluster2 Fyield rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_yield2_bias.xls, append se bdec(3)


*Generating maturity brackets
gen mat=0.25 if durann<=0.375
replace mat=0.5 if durann>0.375 & durann<=0.75
replace mat=1 if durann>0.75 & durann<=1.25
replace mat=1.5 if durann>1.25 & durann<=1.75
replace mat=2 if durann>1.75 & durann<=2.25
replace mat=2.5 if durann>2.25 & durann<=2.75
replace mat=3 if durann>2.75 & durann<=3.25
replace mat=3.5 if durann>3.25 & durann<=3.75
replace mat=4 if durann>3.75 & durann<=4.25
replace mat=4.5 if durann>4.25 & durann<=4.75
replace mat=5 if durann>4.75 & durann<=5.25
replace mat=5.5 if durann>5.25 & durann<=5.75
replace mat=6 if durann>5.75 & durann<=6.5
replace mat=7 if durann>6.5 & durann<=7.5
replace mat=8 if durann>7.5 & durann<=8.5
replace mat=9 if durann>8.5 & durann<=9.5
replace mat=10 if durann>9.5 & durann<=12.5
replace mat=15 if durann>12.5 & durann<=17.5
replace mat=20 if durann>17.5 & durann<=22.5
replace mat=25 if durann>22.5 & durann<=27.5
replace mat=30 if durann>27.5 & durann~=.

*Merging treasury data
sort year mat
merge year mat using "/Users/mkacperc/Jeff/Data/yieldcurve.dta",nokeep
tab _merge
drop _merge

*Creating yield spread
gen yieldspread=Fyield-treasyield


*Regression with coverage interaction
xi: cluster2 yieldspread rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using C:\Jeff\Results\table3_yield22.xls, replace se bdec(3)
xi: cluster2 yieldspread rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using C:\Jeff\Results\table3_yield22.xls, append se bdec(3)
xi: cluster2 yieldspread rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using C:\Jeff\Results\table3_yield22.xls, append se bdec(3)
xi: cluster2 yieldspread rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using C:\Jeff\Results\table3_yield22.xls, append se bdec(3)


sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def00.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def01.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def02.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def03.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def11.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def12.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def13.dta",nokeep
tab _merge
drop _merge

replace default=1 if default==. & (default11==1 | default12==1 | default13==1)
replace default=1 if default==. & (default01==1 | default02==1 | default03==1)

replace default=0 if default==.

xi: cluster2 default rating lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default.xls, replace se bdec(3)
xi: cluster2 default rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default.xls, append se bdec(3)
xi: cluster2 default rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default.xls, append se bdec(3)
xi: cluster2 default rating lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default.xls, append se bdec(3)

xi: cluster2 default rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2.xls, replace se bdec(3)
xi: cluster2 default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2.xls, append se bdec(3)
xi: cluster2 default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2.xls, append se bdec(3)
xi: cluster2 default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2.xls, append se bdec(3)

xi: cluster2 default rating bias inter2 lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2_bias.xls, replace se bdec(3)
xi: cluster2 default rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2_bias.xls, append se bdec(3)
xi: cluster2 default rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2_bias.xls, append se bdec(3)
xi: cluster2 default rating bias inter2 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_default2_bias.xls, append se bdec(3)

*Defining Upgrades and Downgrades

sort permno year
by permno: gen ratchg=rating-rating[_n-1]

*gen upgrade=1 if ratchg>0 & ratchg~=.
*replace upgrade=0 if upgrade==.

gen downgrade=1 if ratchg>0.25 & ratchg<10
replace downgrade=0 if downgrade==.

*gen downgrade=1 if ratchg<0
*replace downgrade=0 if downgrade==.

gen upgrade=1 if ratchg<-0.25 & ratchg>-10
replace upgrade=0 if upgrade==.

xi: cluster2 upgrade rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_upgrade2.xls, replace se bdec(3)
xi: cluster2 upgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_upgrade2.xls, append se bdec(3)
xi: cluster2 upgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_upgrade2.xls, append se bdec(3)
xi: cluster2 upgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_upgrade2.xls, append se bdec(3)

xi: cluster2 downgrade rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_downgrade2.xls, replace se bdec(3)
xi: cluster2 downgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_downgrade2.xls, append se bdec(3)
xi: cluster2 downgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_downgrade2.xls, append se bdec(3)
xi: cluster2 downgrade rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table3_downgrade2.xls, append se bdec(3)


*Summary stats for yield and default
tabstat Fyield default if sum2~=.,stats(mean median sd)

*Regressions in logit specification
logit default rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=.,cluster(permno)
inteff default rating noan inter lnassets lnbmpv retann mktlev tang if sum2~=.
outreg2 using C:\Jeff\Results\table3_default22.xls, replace se bdec(3)
logit default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=.,cluster(permno)
inteff default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=.
outreg2 using C:\Jeff\Results\table3_default22.xls, append se bdec(3)
xi: logit default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=.,cluster(permno)
xi: inteff default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=.
outreg2 using C:\Jeff\Results\table3_default22.xls, append se bdec(3)
xi: logit default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=.,cluster(permno)
xi: inteff default rating noan inter lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=.
outreg2 using C:\Jeff\Results\table3_default22.xls, append se bdec(3)


****************************
*Table 11 (OLS Regressions)*
****************************

egen meannba=mean(nba) if sum2~=.
replace nba=nba-meannba

*Regressions with bond analysts
xi: cluster2 Frating nba lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8.xls, replace se bdec(3)
xi: cluster2 Frating nba lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8.xls, append se bdec(3)
xi: cluster2 Frating nba lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8.xls, append se bdec(3)
xi: cluster2 Frating nba lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8.xls, append se bdec(3)

*Interaction of bias and bond coverage
gen inter4=bias*nba

xi: cluster2 Frating bias nba inter4 lnassets lnbmpv retann mktlev tang if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8_bias.xls, replace se bdec(3)
xi: cluster2 Frating bias nba inter4 lnassets lnbmpv retann mktlev tang dd vol cash sp500 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8_bias.xls, append se bdec(3)
xi: cluster2 Frating bias nba inter4 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8_bias.xls, append se bdec(3)
xi: cluster2 Frating bias nba inter4 lnassets lnbmpv retann mktlev tang dd vol cash sp500 i.sic2 i.year if sum2~=., fcluster(permno) tcluster(year)
outreg2 using \Jeff\Results\table8_bias.xls, append se bdec(3)

************
*IV RESULTS*
************

********************************************
*Creating final data set for IV regressions*
********************************************

use \Bias\test\regdata.dta,clear

rename yeara year

sort permno year
merge permno year using "\Bias\test\controls.dta", nokeep
drop _merge
drop  spsubrating earnass nondiv divbv hhi hhicic instrument1 avginst1 instrument2 avginst2 rtwinst avgrtwinst democratinst klinst

duplicates drop permno year Bias21,force

sort permno year
merge permno year using "/Users/mkacperc/Jeff/DV.dta", nokeep
drop _merge

gen dd=mktlev*vol

gen sum=noan+lnassets+lnbmpv+retann+sp500+mktlev+vol+dd
gen sum2=Frating+noan+lnassets+lnbmpv+retann+sp500+mktlev+vol+dd+durann+lnbro+Bias21+Rec21+tang

save "/Users/mkacperc/Jeff/reginst.dta",replace
**********************************************************************************************************

*Extracting face value of debt issued

use "/Users/mkacperc/Jeff/final_ann.dta",clear
tsset permno year

keep permno year sumface

drop if sumface==.
drop if permno==.
sort permno year

save "/Users/mkacperc/Jeff/face.dta",replace

clear all
use "/Users/mkacperc/Jeff/reginst.dta"

*********************************************
*Table 4 (Summary Statistics for Experiment)*
*********************************************

replace sum2=Frating+noan+lnassets+lnbmpv+retann+sp500+mktlev+vol+dd+durann+lnbro+Bias21+Rec21+tang+cash

tabstat Frating noan nba lnassets lnbmpv retann sp500 mktlev vol dd tang cash if sum2~=.,stats(mean median sd)

sort permno merger
merge permno merger using "/Users/mkacperc/Jeff/Kingsley/stopages.dta",nokeep
drop _merge

gen ST=stock
replace ST=0 if stopped==1
gen INT=ST*dum

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/BA.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/face.dta",nokeep
tab _merge
drop _merge

rename sumface face

gen nba2=nba
*replace nba2=0 if nba2==. & noan~=.
replace nba2=0 if nba2==. & noan~=. & year>2001

gen sic2=int(sic3/10)

****************************************
*Table 5a (coverage+rating regressions)*
****************************************

xi:reg noan dum stock inter lnassets lnbmpv retann mktlev tang i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par.xls, replace se bdec(3)
xi:reg noan dum stock inter lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par.xls, append se bdec(3)

xi:reg noan dum ST INT lnassets lnbmpv retann mktlev tang i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par2.xls, replace se bdec(3)
xi:reg noan dum ST INT lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par2.xls, append se bdec(3)
xi:areg noan dum ST INT lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5a_par2.xls, append se bdec(3)
xi:reg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par2.xls, append se bdec(3)
xi:reg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_par2.xls, append se bdec(3)
xi:areg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5a_par2.xls, append se bdec(3)

*************************************************
*Table 5b (conditioning on coverage regressions)*
*************************************************

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & noan<5, cluster(merger)
outreg2 using \Jeff\Results\table5b_par.xls, replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & noan>=5&noan<20, cluster(merger)
outreg2 using \Jeff\Results\table5b_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & noan>=20, cluster(merger)
outreg2 using \Jeff\Results\table5b_par.xls, append se bdec(3)

xi:areg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan<5, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par.xls, replace se bdec(3)
xi:areg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=5&noan<20, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par.xls, append se bdec(3)
xi:areg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=20, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par.xls, append se bdec(3)

xi:reg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan<5, cluster(merger)
outreg2 using \Jeff\Results\table5b_par2.xls, replace se bdec(3)
xi:reg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=5&noan<20, cluster(merger)
outreg2 using \Jeff\Results\table5b_par2.xls, append se bdec(3)
xi:reg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=20, cluster(merger)
outreg2 using \Jeff\Results\table5b_par2.xls, append se bdec(3)

xi:areg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan<5, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par2.xls, replace se bdec(3)
xi:areg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=5&noan<20, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par2.xls, append se bdec(3)
xi:areg Frating dum ST INT noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=. & noan>=20, cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table5b2_par2.xls, append se bdec(3)


*************************************************
*Table 5b (conditioning on bond coverage regressions)*
*************************************************

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & nba2<=2, cluster(merger)
outreg2 using \Jeff\Results\table5a_bondcov.xls, replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & nba2>2 & nba2~=., cluster(merger)
outreg2 using \Jeff\Results\table5a_bondcov.xls, append se bdec(3)


*Table xxx (extended rating regressions)
*xi:reg Frating dum stock inter noan lnassets lnbmpv retann sp500 mktlev vol dd durann lnbro Bias21 Rec21 i.merger i.year if sum2~=., cluster(merger)
*outreg2 using \Jeff\table_iv_rating.txt, append se bdec(3) coefastr
*xi:reg Frating dum ST INT noan lnassets lnbmpv retann sp500 mktlev vol dd durann lnbro Bias21 Rec21 i.merger i.year if sum2~=., cluster(merger)
*outreg2 using \Jeff\table_iv_rating.txt, append se bdec(3) coefastr

drop sum2
gen sum2=Frating+noan+lnassets+lnbmpv+retann+sp500+mktlev+vol+dd+durann+lnbro+Bias21+Rec21

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/totalcoverage.dta",nokeep
tab _merge
drop _merge

********************************************
*Conditioning on distress - breaking sample*
********************************************

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/anndefault.dta",nokeep
tab _merge

drop _merge

sort permno year
by permno: replace naiveedf=naiveedf[_n-1] if naiveedf==. & naiveedf[_n-1]~=. & year>2003
by permno: replace naivedd=naivedd[_n-1] if naivedd==. & naivedd[_n-1]~=. & year>2003

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & rating<=10, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & rating>10, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & naivedd<5, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & naivedd>=5, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & dd<0.05, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & dd>=0.11, cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)



****************************************
*Conditioning on distress - triple diff*
****************************************

gen inter31=inter*dd
gen inter32=inter*mktlev
gen inter33=inter*vol
gen inter21=dum*dd
gen inter22=dum*mktlev
gen inter23=dum*vol
gen inter11=stock*dd
gen inter12=stock*mktlev
gen inter13=stock*vol

gen intnoan1=noan*dd
gen intnoan2=noan*mktlev
gen intnoan3=noan*vol
gen intass1=lnassets*dd
gen intass2=lnassets*mktlev
gen intass3=lnassets*vol
gen intbm1=lnbmpv*dd
gen intbm2=lnbmpv*mktlev
gen intbm3=lnbmpv*vol
gen intret1=retann*dd
gen intret2=retann*mktlev
gen intret3=retann*vol
gen inttang1=tang*dd
gen inttang2=tang*mktlev
gen inttang3=tang*vol
gen intsp1=sp500*dd
gen intsp2=sp500*mktlev
gen intsp3=sp500*vol

xi:reg Frating dum stock inter inter21 inter11 inter31 noan intnoan1 lnassets intass1 lnbmpv intbm1 retann intret1 mktlev tang inttang1 dd vol sp500 intsp1 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, replace se bdec(3)
xi:reg Frating dum stock inter inter22 inter12 inter32 noan lnassets lnbmpv retann mktlev tang dd vol sp500 intnoan2 intass2 intbm2 intret2 inttang2 intsp2 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)
xi:reg Frating dum stock inter inter23 inter13 inter33 noan lnassets lnbmpv retann mktlev tang dd vol sp500 intnoan3 intass3 intbm3 intret3 inttang3 intsp3 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table9a_par.xls, append se bdec(3)


********************************************
*Conditioning on size - breaking sample*
********************************************

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets<8.5, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_size.xls", replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>=8.5, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_size.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol i.year i.merger i.sic2 if sum2~=. & sp500==0, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_size.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & sp500==1, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_size.xls", append se bdec(3)

*******************************************************************
*Conditioning on face value of debt and leverage - breaking sample*
*******************************************************************

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & face<300000, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & face>=300000 & face~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & mktlev<0.3612, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & mktlev>=0.3612 & mktlev~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & booklev<0.3275, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & booklev>=0.3275 & booklev~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debt.xls", append se bdec(3)

*****************************************************************
*Conditioning on finance industry and the level of debt issuance*
*****************************************************************

gen sic1=int(sic2/10)

gen fin=1 if sic1==6
replace fin=0 if fin==.

gen facedum=1 if face>=300000 & face~=.
replace facedum=0 if face<300000

gen mktlevdum=1 if mktlev>=0.36 & mktlev~=.
replace mktlevdum=0 if mktlev<0.36

gen booklevdum=1 if booklev>=0.33 & booklev~=.
replace booklevdum=0 if booklev<0.33

gen intfin1=dum*fin
gen intfin2=stock*fin
gen intfin3=inter*fin

gen intface1=dum*facedum
gen intface2=stock*facedum
gen intface3=inter*facedum

gen intmktlev1=dum*mktlevdum
gen intmktlev2=stock*mktlevdum
gen intmktlev3=inter*mktlevdum

gen intbooklev1=dum*booklevdum
gen intbooklev2=stock*booklevdum
gen intbooklev3=inter*booklevdum

tabulate sic1,gen(sicdum)

forvalues i=1(1)10{
gen intsic1`i'=dum*sicdum`i'
gen intsic2`i'=stock*sicdum`i'
gen intsic3`i'=inter*sicdum`i'
}

forvalues i=1(1)10{
gen sizesic1`i'=lnassets*sicdum`i'
}

xi:reg Frating dum stock inter facedum fin intfin1 intfin2 intfin3 intface1 intface2 intface3 noan lnassets lnbmpv retann tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debtfin.xls", replace se bdec(3)
xi:reg Frating dum stock inter fin mktlevdum intfin1 intfin2 intfin3 intmktlev1 intmktlev2 intmktlev3 noan lnassets lnbmpv retann tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debtfin.xls", append se bdec(3)
xi:reg Frating dum stock inter fin booklevdum intfin1 intfin2 intfin3 intbooklev1 intbooklev2 intbooklev3 noan lnassets lnbmpv retann tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debtfin.xls", append se bdec(3)
xi:reg Frating dum stock inter fin intfin1 intfin2 intfin3 noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_debtfin.xls", append se bdec(3)

*Using Fama French 12 industries
gen ff12=1 if siccd>=100 & siccd<=999
replace ff12=1 if siccd>=2000 & siccd<=2399
replace ff12=1 if siccd>=2700 & siccd<=2749
replace ff12=1 if siccd>=2770 & siccd<=2799
replace ff12=1 if siccd>=3100 & siccd<=3199
replace ff12=1 if siccd>=3940 & siccd<=3989

replace ff12=2 if siccd>=2500 & siccd<=2519
replace ff12=2 if siccd>=2590 & siccd<=2599
replace ff12=2 if siccd>=3630 & siccd<=3659
replace ff12=2 if siccd>=3710 & siccd<=3711
replace ff12=2 if siccd>=3714 & siccd<=3714
replace ff12=2 if siccd>=3716 & siccd<=3716
replace ff12=2 if siccd>=3750 & siccd<=3751
replace ff12=2 if siccd>=3792 & siccd<=3792
replace ff12=2 if siccd>=3900 & siccd<=3939
replace ff12=2 if siccd>=3990 & siccd<=3999

replace ff12=3 if siccd>=2520 & siccd<=2589
replace ff12=3 if siccd>=2600 & siccd<=2699
replace ff12=3 if siccd>=2750 & siccd<=2769
replace ff12=3 if siccd>=3000 & siccd<=3099
replace ff12=3 if siccd>=3200 & siccd<=3569
replace ff12=3 if siccd>=3580 & siccd<=3629
replace ff12=3 if siccd>=3700 & siccd<=3709
replace ff12=3 if siccd>=3712 & siccd<=3713
replace ff12=3 if siccd>=3715 & siccd<=3715
replace ff12=3 if siccd>=3717 & siccd<=3749
replace ff12=3 if siccd>=3752 & siccd<=3791
replace ff12=3 if siccd>=3793 & siccd<=3799
replace ff12=3 if siccd>=3830 & siccd<=3839
replace ff12=3 if siccd>=3860 & siccd<=3899

replace ff12=4 if siccd>=1200 & siccd<=1399
replace ff12=4 if siccd>=2900 & siccd<=2999

replace ff12=5 if siccd>=2800 & siccd<=2829
replace ff12=5 if siccd>=2840 & siccd<=2899

replace ff12=6 if siccd>=3570 & siccd<=3579
replace ff12=6 if siccd>=3660 & siccd<=3692
replace ff12=6 if siccd>=3694 & siccd<=3699
replace ff12=6 if siccd>=3810 & siccd<=3829
replace ff12=6 if siccd>=7370 & siccd<=7379

replace ff12=7 if siccd>=4800 & siccd<=4899

replace ff12=8 if siccd>=4900 & siccd<=4949

replace ff12=9 if siccd>=5000 & siccd<=5999
replace ff12=9 if siccd>=7200 & siccd<=7299
replace ff12=9 if siccd>=7600 & siccd<=7699

replace ff12=10 if siccd>=2830 & siccd<=2839
replace ff12=10 if siccd>=3693 & siccd<=3693
replace ff12=10 if siccd>=3840 & siccd<=3859
replace ff12=10 if siccd>=8000 & siccd<=8099

replace ff12=11 if siccd>=6000 & siccd<=6999

replace ff12=12 if ff12==.

tabulate ff12,gen(ff12dum)

forvalues i=1(1)12{
gen intff1`i'=dum*ff12dum`i'
gen intff2`i'=stock*ff12dum`i'
gen intff3`i'=inter*ff12dum`i'
}

*All tests with the benchmark of finance industry

xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan i.year i.merger if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic.xls", replace se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic.xls", append se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic.xls", append se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 intface* facedum noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic.xls", append se bdec(3)

xi:reg Frating dum stock inter ff12dum* intff11-intff110 intff112 intff21-intff210 intff212 intff31-intff310 intff312 noan i.year i.merger if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finff.xls", replace se bdec(3)
xi:reg Frating dum stock inter ff12dum* intff11-intff110 intff112 intff21-intff210 intff212 intff31-intff310 intff312 noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finff.xls", append se bdec(3)
xi:reg Frating dum stock inter ff12dum* intff11-intff110 intff112 intff21-intff210 intff212 intff31-intff310 intff312 noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finff.xls", append se bdec(3)
xi:reg Frating dum stock inter ff12dum* intff11-intff110 intff112 intff21-intff210 intff212 intff31-intff310 intff312 intface* facedum noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finff.xls", append se bdec(3)

*Conditioning on large cap
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan i.year i.merger if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large.xls", replace se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large.xls", append se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large.xls", append se bdec(3)
xi:reg Frating dum stock inter sicdum* intsic11-intsic16 intsic18-intsic110 intsic21-intsic26 intsic28-intsic210 intsic31-intsic36 intsic38-intsic310 intface* facedum noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large.xls", append se bdec(3)

gen intfin11=fin*dum
gen intfin21=fin*stock
gen intfin31=fin*inter

*Conditioning on large cap
xi:reg Frating dum stock inter fin intfin11 intfin21 intfin31 noan i.year i.merger if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large_fin.xls", replace se bdec(3)
xi:reg Frating dum stock inter fin intfin11 intfin21 intfin31 noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large_fin.xls", append se bdec(3)
xi:reg Frating dum stock inter fin intfin11 intfin21 intfin31 noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large_fin.xls", append se bdec(3)
xi:reg Frating dum stock inter fin intfin11 intfin21 intfin31 intface* facedum noan lnassets lnbmpv retann booklev tang dd vol sp500 i.year i.merger i.sic2 if sum2~=. & lnassets>8.41 & lnassets~=., cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finsic_large_fin.xls", append se bdec(3)

xi:areg Bias21 dum stock inter ff12dum* intff11-intff110 intff112 intff21-intff210 intff212 intff31-intff310 intff312 noan lnsize lnbmpv retann booklev dd vol sp500  i.merger if sum2~=., absorb(sic2) cluster(merger)

**************************
*Looking at the finance industry and credit ratings explicitly
**************************

xi:reg Frating lnbmpv retann mktlev tang dd vol sp500 i.year i.sic1, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", replace se bdec(3)
xi:reg Frating lnbmpv lnassets retann mktlev tang dd vol sp500 i.year i.sic1, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", append se bdec(3)
xi:reg Frating lnbmpv retann mktlev tang dd vol sp500 i.year i.sic1 if lnassets>8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", append se bdec(3)
xi:reg Frating lnbmpv retann mktlev tang dd vol sp500 i.year i.sic1 if lnassets<=8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", append se bdec(3)

**************************************************************
*Looking at finance industry relative to all other industries*
**************************************************************
xi:reg Frating  sicdum7 lnbmpv retann mktlev tang dd vol sp500 i.year, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", replace se bdec(3)
xi:reg Frating  sicdum7 lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets<=8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", append se bdec(3)
xi:reg Frating  sicdum7 lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets>8.41 & lnassets~=., nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", append se bdec(3)
xi:reg Frating  sicdum7 lnassets lnbmpv retann mktlev tang dd vol sp500 i.year, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", append se bdec(3)
xi:reg Frating  sicdum7 lnassets lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets<=8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", append se bdec(3)
xi:reg Frating  sicdum7 lnassets lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets>8.41 & lnassets~=., nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finrest.xls", append se bdec(3)

*******************************************************************************
*Looking at finance industry relative to next most optimistic industry (SIC 4)*
*******************************************************************************

gen gdum=sicdum5+sicdum7

xi:reg Frating  sicdum7 gdum lnbmpv retann mktlev tang dd vol sp500 i.year, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", replace se bdec(3)
xi:reg Frating  sicdum7 gdum lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets<=8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", append se bdec(3)
xi:reg Frating  sicdum7 gdum lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets>8.41 & lnassets~=., nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", append se bdec(3)
xi:reg Frating  sicdum7 gdum lnassets lnbmpv retann mktlev tang dd vol sp500 i.year, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", append se bdec(3)
xi:reg Frating  sicdum7 gdum lnassets lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets<=8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", append se bdec(3)
xi:reg Frating  sicdum7 gdum lnassets lnbmpv retann mktlev tang dd vol sp500 i.year if lnassets>8.41 & lnassets~=., nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finnext.xls", append se bdec(3)


************************************************************************
*Looking at the finance industry and credit ratings conditional on size*
************************************************************************

xi:reg Frating lnbmpv lnassets retann mktlev tang dd vol sp500 i.year sicdum* sizesic1-sizesic10 if lnassets<8.41, nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", replace se bdec(3)
xi:reg Frating lnbmpv lnassets retann mktlev tang dd vol sp500 i.year i.sic1 if lnassets>8.41 & lnassets~=., nocons cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_finpred.xls", append se bdec(3)
xi:reg Frating lnbmpv lnassets retann mktlev tang dd vol sp500 i.year sicdum2-sicdum6 sicdum7-sicdum10 sizesic12-sizesic16  sizesic17-sizesic110, nocons cluster(merger)

gen sizedum=1 if lnassets>8.41 & lnassets~=.
replace sizedum=0 if lnassets<8.41

forvalues i=1(1)10{
gen sizedumsic1`i'=sizedum*sicdum`i'
}

xi:reg Frating lnbmpv sizedum retann mktlev tang dd vol sp500 i.year sicdum1-sicdum6 sicdum7-sicdum9 sizedumsic11-sizedumsic16  sizedumsic17-sizedumsic19, nocons cluster(merger)

*********************************************
*Controlling for Bias, Accuracy, and Recency*
*********************************************

xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash Bias21 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table11_par.xls, replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash Acc21 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table11_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash Rec21 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table11_par.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash Bias21 Acc21 Rec21 i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table11_par.xls, append se bdec(3)

**********************
*Staleness of ratings*
**********************

xi:reg Frating dum noan lnassets lnbmpv retann mktlev tang i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table_stale.xls, replace se bdec(3)
xi:reg Frating dum noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table_stale.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table_stale.xls, append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table_stale.xls, append se bdec(3)

********************************
*Conditioning on SIC-1 industry*
********************************

gen sic1=int(sic2/10)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==1, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", replace se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==2, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==3, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==4, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==5, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==6, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==7, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)
xi:reg Frating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 cash i.year i.merger i.sic2 if sum2~=. & sic1==8, cluster(merger)
outreg2 using "/Users/mkacperc/Jeff/Results/table_sic1.xls", append se bdec(3)

*****************************************
*Using firm-level ratings from Compustat*
*****************************************

sort permno year
by permno: gen Fsprating=sprating[_n+1]

xi:reg Fsprating dum stock inter noan lnassets lnbmpv retann mktlev tang i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table10_par.xls, append se bdec(3)
xi:reg Fsprating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger)
outreg2 using \Jeff\Results\table10_par.xls, append se bdec(3)
xi:areg Fsprating dum stock inter noan lnassets lnbmpv retann mktlev tang dd vol sp500 i.year i.merger if sum2~=., cluster(merger) absorb(permno)
outreg2 using \Jeff\Results\table10_par.xls, append se bdec(3)

*************************************************************
*Estimating the effect for yields using data on bonds*
*************************************************************

clear
use "/Users/mkacperc/Jeff/Data/bonds yield return.dta"

replace cusip = subinstr(cusip, "'", "",.) 

sort cusip year
merge cusip year using \Jeff\Data\cusip_permno,nokeep
tab _merge

drop _merge cusip2 firmname
rename permno2 permno

sort permno year month

drop if yield<=0

by permno year: egen annyield=mean(yield)
by permno year: egen annreturn=mean(return)

keep permno year annyield annreturn

duplicates drop
tsset permno year

gen Fannyield=F.annyield
gen Fannreturn=F.annreturn

sort permno year

save \Jeff\Data\bondyields.dta,replace

***************************
*Merging Agency Paper Data*
***************************

clear all
use \Jeff\Data\agency_data.dta

sort permno year
by permno year: egen totsize=sum(outstd)
gen weight=outstd/totsize

gen prodyield=weight*yield
gen prodret=weight*return
gen prodcoupon=weight*coupon
gen prodduration=weight*duration
gen prodconvex=weight*convex
gen prodbid=weight*bid

by permno year: egen vwyield=sum(prodyield)
by permno year: egen vwret=sum(prodret)
by permno year: egen vwcoupon=sum(prodcoupon)
by permno year: egen vwduration=sum(prodduration)
by permno year: egen vwconvex=sum(prodconvex)
by permno year: egen vwbid=sum(prodbid)

drop weight prod*

keep permno year vw*

duplicates drop

tsset permno year

gen Fvwyield=F.vwyield
gen Fvwbid=F.vwbid
gen Fvwret=F.vwret

tsset permno year

save \Jeff\Data\agency_inputs.dta,replace


clear all
use "/Users/mkacperc/Jeff/reginst.dta"

sort permno merger
merge permno merger using "/Users/mkacperc/Jeff/Kingsley/stopages.dta",nokeep
drop _merge

gen ST=stock
replace ST=0 if stopped==1
gen INT=ST*dum

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/BA.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/agency_inputs.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/bondyields.dta",nokeep
tab _merge
drop _merge

gen Fyield=Fannyield
*replace Fyield=Fvwyield if Fyield==. & Fvwyield~=.

winsor Fyield,gen(WFyield) p(0.01)
winsor Fyield,gen(WFyield2) p(0.025)

winsor annyield,gen(Wyield) p(0.01)
winsor annyield,gen(Wyield2) p(0.025)
winsor annyield,gen(Wyield3) p(0.005)

gen sic2=int(sic3/10)

xi:reg annyield dum stock inter lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2 if Frating~=., cluster(merger)
outreg2 using \Jeff\Results\table12.xls, replace se bdec(3)
xi:reg annyield dum stock inter lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2 if Frating~=. & noan<5, cluster(merger)
outreg2 using \Jeff\Results\table12.xls, append se bdec(3)
xi:reg annyield dum stock inter lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2 if Frating~=. & noan>=5 & noan<20, cluster(merger)
outreg2 using \Jeff\Results\table12.xls, append se bdec(3)
xi:reg annyield dum stock inter lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2 if Frating~=. & noan>=20, cluster(merger)
outreg2 using \Jeff\Results\table12.xls, append se bdec(3)

**********************************************
*Estimating the model with yields and ratings*
**********************************************

*Generating interaction effects
gen int1=dum*rating
gen int2=stock*rating
rename inter int0
gen inter3=dum*rating*stock

*Splitting the sample by treated and post
reg annyield rating if dum==0 & stock==0, cluster(merger)
reg annyield rating if dum==1 & stock==0, cluster(merger)
reg annyield rating if dum==0 & stock==1, cluster(merger)
reg annyield rating if dum==1 & stock==1, cluster(merger)

reg annyield rating int1 int2 int0 inter3 dum stock, cluster(merger)
xi: reg annyield rating int1 int2 int0 inter3 dum stock i.merger, cluster(merger)
xi: areg annyield rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
xi: areg annyield rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)

*DID Regression
xi: areg annyield rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, replace se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)
xi: areg annyield rating int1 int2 int0 inter3 dum stock i.merger i.sic2 i.year,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_precision.xls, append se bdec(3)

*Generating maturity brackets
gen mat=0.25 if durann<=0.375
replace mat=0.5 if durann>0.375 & durann<=0.75
replace mat=1 if durann>0.75 & durann<=1.25
replace mat=1.5 if durann>1.25 & durann<=1.75
replace mat=2 if durann>1.75 & durann<=2.25
replace mat=2.5 if durann>2.25 & durann<=2.75
replace mat=3 if durann>2.75 & durann<=3.25
replace mat=3.5 if durann>3.25 & durann<=3.75
replace mat=4 if durann>3.75 & durann<=4.25
replace mat=4.5 if durann>4.25 & durann<=4.75
replace mat=5 if durann>4.75 & durann<=5.25
replace mat=5.5 if durann>5.25 & durann<=5.75
replace mat=6 if durann>5.75 & durann<=6.5
replace mat=7 if durann>6.5 & durann<=7.5
replace mat=8 if durann>7.5 & durann<=8.5
replace mat=9 if durann>8.5 & durann<=9.5
replace mat=10 if durann>9.5 & durann<=12.5
replace mat=15 if durann>12.5 & durann<=17.5
replace mat=20 if durann>17.5 & durann<=22.5
replace mat=25 if durann>22.5 & durann<=27.5
replace mat=30 if durann>27.5 & durann~=.

*Merging treasury data
sort year mat
merge year mat using \Jeff\Data\yieldcurve.dta,nokeep
tab _merge
drop _merge

*Creating yield spread
gen yieldspread=annyield-treasyield

*DID Regression
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, replace se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)
xi: areg yieldspread rating int1 int2 int0 inter3 dum stock i.merger i.sic2 i.year,absorb(permno) cluster(merger)
outreg2 using C:\Jeff\Results\table_precision2.xls, append se bdec(3)

*CONTINUE AFTER THIS FUNCTION!!!

****************************************************************************************************************

*********************************
*Creating yield spread variables*
*********************************

clear all
set mem 4g
use "/Users/mkacperc/Jeff/Data/Yield Curve.dta"
rename  Ten_Year Y1
rename  Three_Month Y2
rename  Five_Year Y3
rename  One_Year Y4
rename  Twenty_Year Y5
rename  Thirty_Year Y6
rename Three_Year Y7
rename  Two_Year Y8
rename  Seven_Year Y9
rename  One_Month Y10
rename  Six_Month Y11
replace Y5=Y1+(Y6-Y1)/2 if Y5==.

gen Y12=Y4+(Y8-Y4)/2
gen Y13=Y8+(Y7-Y8)/2
gen Y14=Y7+(Y3-Y7)/2
gen Y15=Y3+(Y9-Y3)/2
gen Y16=Y9+(Y1-Y9)/3
gen Y17=Y9+2*(Y1-Y9)/3
gen Y18=Y1+(Y5-Y1)/2
gen Y19=Y1+(Y18-Y1)/5
gen Y20=Y1+2*(Y18-Y1)/5
gen Y21=Y1+3*(Y18-Y1)/5
gen Y22=Y1+4*(Y18-Y1)/5
gen Y23=Y7+(Y14-Y7)/2
gen Y24=Y14+(Y3-Y14)/2
gen Y25=Y3+(Y15-Y3)/2
gen Y26=Y5+(Y6-Y5)/2
replace Y26=Y5+(Y5-Y18) if Y26==.
replace Y6=Y5+2*(Y5-Y18) if Y6==.
replace Y11=Y2+(Y4-Y2)/3 if Y11==.


gen year=substr(Date,1,4)
destring year,replace force
sort year Date
by year: egen Yann1=mean(Y1)
by year: egen Yann2=mean(Y2)
by year: egen Yann3=mean(Y3)
by year: egen Yann4=mean(Y4)
by year: egen Yann5=mean(Y5)
by year: egen Yann6=mean(Y6)
by year: egen Yann7=mean(Y7)
by year: egen Yann8=mean(Y8)
by year: egen Yann9=mean(Y9)
by year: egen Yann10=mean(Y10)
by year: egen Yann11=mean(Y11)
by year: egen Yann12=mean(Y12)
by year: egen Yann13=mean(Y13)
by year: egen Yann14=mean(Y14)
by year: egen Yann15=mean(Y15)
by year: egen Yann16=mean(Y16)
by year: egen Yann17=mean(Y17)
by year: egen Yann18=mean(Y18)
by year: egen Yann19=mean(Y19)
by year: egen Yann20=mean(Y20)
by year: egen Yann21=mean(Y21)
by year: egen Yann22=mean(Y22)
by year: egen Yann23=mean(Y23)
by year: egen Yann24=mean(Y24)
by year: egen Yann25=mean(Y25)
by year: egen Yann26=mean(Y26)

keep year Yann*
duplicates drop

reshape long Yann,i(year) j(maturity)
gen mat=10 if maturity==1
replace mat=3/12 if maturity==2
replace mat=5 if maturity==3
replace mat=1 if maturity==4
replace mat=20 if maturity==5
replace mat=30 if maturity==6
replace mat=3 if maturity==7
replace mat=2 if maturity==8
replace mat=7 if maturity==9
replace mat=1/12 if maturity==10
replace mat=0.5 if maturity==11
replace mat=1.5 if maturity==12
replace mat=2.5 if maturity==13
replace mat=4 if maturity==14
replace mat=6 if maturity==15
replace mat=8 if maturity==16
replace mat=9 if maturity==17
replace mat=15 if maturity==18
replace mat=11 if maturity==19
replace mat=12 if maturity==20
replace mat=13 if maturity==21
replace mat=14 if maturity==22
replace mat=3.5 if maturity==23
replace mat=4.5 if maturity==24
replace mat=5.5 if maturity==25
replace mat=25 if maturity==26

rename Yann treasyield
drop maturity
sort year mat

save "/Users/mkacperc/Jeff/Data/yieldcurve.dta",replace

********************************************************************

****************************
*Merging data with defaults*
****************************

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def00.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def01.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def02.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def03.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def11.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def12.dta",nokeep
tab _merge
drop _merge

sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/def13.dta",nokeep
tab _merge
drop _merge

replace default=1 if default==. & dum==1 & (default11==1 | default12==1 | default13==1)
replace default=1 if default==. & dum==0 & (default01==1 | default02==1 | default03==1)

replace default=0 if default==.

*Linear Probability Model with Default
xi: areg default rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, replace se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)
xi: areg default rating int1 int2 int0 inter3 dum stock i.merger i.sic2 i.year,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_default.xls, append se bdec(3)

*Defining Upgrades and Downgrades

sort permno year
by permno: gen ratchg=rating-rating[_n-1]

*gen upgrade=1 if ratchg>0 & ratchg~=.
*replace upgrade=0 if upgrade==.

gen upgrade=1 if ratchg>0.25 & ratchg<10
replace upgrade=0 if upgrade==.

*gen downgrade=1 if ratchg<0
*replace downgrade=0 if downgrade==.

gen downgrade=1 if ratchg<-0.25 & ratchg>-10
replace downgrade=0 if downgrade==.

*gen grade=upgrade+downgrade

gen grade=1 if abs(ratchg)>0.25 & abs(ratchg)<10
replace grade=0 if grade==.

*Merging default and grade together
gen defgrade=default+grade
replace defgrade=1 if defgrade==2

*Demeaning the control variables
sort permno
by permno: egen meanass=mean(lnassets) if sum2~=.
by permno: egen meanret=mean(retann) if sum2~=.
by permno: egen meanlev=mean(mktlev) if sum2~=.
by permno: egen meandd=mean(dd) if sum2~=.
by permno: egen meanvol=mean(vol) if sum2~=.
by permno: egen meansp500=mean(sp500) if sum2~=.
by permno: egen meancash=mean(cash) if sum2~=.
by permno: egen meandur=mean(durann) if sum2~=.
by permno: egen meanrat=mean(rating) if sum2~=.
by permno: egen meandef=mean(default) if sum2~=.
by permno: egen meanupgrade=mean(upgrade) if sum2~=.
by permno: egen meandown=mean(downgrade) if sum2~=.
by permno: egen meanint1=mean(int1) if sum2~=.
by permno: egen meanint2=mean(int2) if sum2~=.
by permno: egen meanint0=mean(int0) if sum2~=.
by permno: egen meaninter3=mean(inter3) if sum2~=.
by permno: egen meandum=mean(dum) if sum2~=.
by permno: egen meanstock=mean(stock) if sum2~=.


gen int1=dum*rating
gen int2=stock*rating
rename inter int0
gen inter3=dum*rating*stock


gen lnassetsfe=lnassets-meanass
gen retannfe=retann-meanret
gen mktlevfe=mktlev-meanlev
gen ddfe=dd-meandd
gen volfe=vol-meanvol
gen sp500fe=sp500-meansp500
gen cashfe=cash-meancash
gen durannfe=durann-meandur
gen ratingfe=rating-meanrat
gen defaultfe=default-meandef
gen upgradefe=upgrade-meanupgrade
gen downgradefe=downgrade-meandown
gen dumfe=dum-meandum
gen stockfe=stock-meanstock
gen int1fe=dumfe*ratingfe
gen int2fe=stockfe*ratingfe
gen int0fe=dumfe*stockfe
gen inter3fe=dumfe*ratingfe*stockfe

*Estimating logit model
xi: logit default int1 stock inter3 int2 int0 rating dum i.merger i.sic2,cluster(merger)
xi: inteff default int1 stock inter3 int2 int0 rating dum i.merger i.sic2
outreg2 using \Jeff\Results\table_default2.xls, replace se bdec(3)
xi: logit default int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2,cluster(merger)
xi: inteff default int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)
xi: logit default int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2, cluster(merger)
xi: inteff default int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)
xi: logit defaultfe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2, cluster(merger)
xi: inteff defaultfe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)
xi: logit defaultfe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2, cluster(merger)
xi: inteff defaultfe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)


xi: logit default int1 stock inter3 int2 int0 rating dum i.merger i.sic2 i.permno, cluster(merger)
xi: inteff default int1 stock inter3 int2 int0 rating dum i.merger i.sic2 i.permno
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)
xi: logit default int1 stock inter3 int2 int0 rating dum i.merger i.sic2 i.year i.permno, cluster(merger)
xi: inteff default int1 stock inter3 int2 int0 rating dum i.merger i.sic2 i.year i.permno
outreg2 using \Jeff\Results\table_default2.xls, append se bdec(3)

*Linear Probability Model with Upgrades and Downgrades
xi: areg upgrade rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_upgrade.xls, replace se bdec(3)
xi: areg upgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_upgrade.xls, append se bdec(3)
xi: areg upgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_upgrade.xls, append se bdec(3)
xi: areg upgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_upgrade.xls, append se bdec(3)
xi: areg upgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_upgrade.xls, append se bdec(3)

xi: areg downgrade rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_downgrade.xls, replace se bdec(3)
xi: areg downgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_downgrade.xls, append se bdec(3)
xi: areg downgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_downgrade.xls, append se bdec(3)
xi: areg downgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_downgrade.xls, append se bdec(3)
xi: areg downgrade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_downgrade.xls, append se bdec(3)

xi: areg grade rating int1 int2 int0 inter3 dum stock i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_grade.xls, replace se bdec(3)
xi: areg grade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_grade.xls, append se bdec(3)
xi: areg grade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger,absorb(sic2) cluster(merger)
outreg2 using \Jeff\Results\table_grade.xls, append se bdec(3)
xi: areg grade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_grade.xls, append se bdec(3)
xi: areg grade rating int1 int2 int0 inter3 dum stock lnassets retann mktlev tang dd vol sp500 durann cash i.year i.merger i.sic2,absorb(permno) cluster(merger)
outreg2 using \Jeff\Results\table_grade.xls, append se bdec(3)


*Estimating logit model (upgrades)
xi: logit upgrade int1 stock inter3 int2 int0 rating dum i.merger i.sic2,cluster(merger)
xi: inteff upgrade int1 stock inter3 int2 int0 rating dum i.merger i.sic2
outreg2 using \Jeff\Results\table_upgrade2.xls, replace se bdec(3)
xi: logit upgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2,cluster(merger)
xi: inteff upgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2
outreg2 using \Jeff\Results\table_upgrade2.xls, append se bdec(3)
xi: logit upgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2, cluster(merger)
xi: inteff upgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2
outreg2 using \Jeff\Results\table_upgrade2.xls, append se bdec(3)
xi: logit upgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2, cluster(merger)
xi: inteff upgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2
outreg2 using \Jeff\Results\table_upgrade2.xls, append se bdec(3)
xi: logit upgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2, cluster(merger)
xi: inteff upgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2
outreg2 using \Jeff\Results\table_upgrade2.xls, append se bdec(3)

*Estimating logit model (downgrades)
xi: logit downgrade int1 stock inter3 int2 int0 rating dum i.merger i.sic2,cluster(merger)
xi: inteff downgrade int1 stock inter3 int2 int0 rating dum i.merger i.sic2
outreg2 using \Jeff\Results\table_downgrade2.xls, replace se bdec(3)
xi: logit downgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2,cluster(merger)
xi: inteff downgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev i.merger i.sic2
outreg2 using \Jeff\Results\table_downgrade2.xls, append se bdec(3)
xi: logit downgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2, cluster(merger)
xi: inteff downgrade int1 stock inter3 int2 int0 rating dum lnassets retann mktlev dd vol sp500 durann cash i.merger i.sic2
outreg2 using \Jeff\Results\table_downgrade2.xls, append se bdec(3)
xi: logit downgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2, cluster(merger)
xi: inteff downgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.merger i.sic2
outreg2 using \Jeff\Results\table_downgrade2.xls, append se bdec(3)
xi: logit downgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2, cluster(merger)
xi: inteff downgradefe int1fe stockfe inter3fe int2fe int0fe ratingfe dumfe lnassetsfe retannfe mktlevfe ddfe volfe sp500fe durannfe cashfe i.year i.merger i.sic2
outreg2 using \Jeff\Results\table_downgrade2.xls, append se bdec(3)



*Summary stats for yield and default
tabstat annyield default if sum2~=.,stats(mean median sd)

**************************************
*Interactions of rating with industry*
**************************************

forvalues i=1(1)10 {
gen sicdumrat`i'=sicdum`i'*rating
}

xi: reg default sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", replace se bdec(3)
xi: reg upgrade sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", append se bdec(3)
xi: reg downgrade sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", append se bdec(3)
xi: reg default sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", append se bdec(3)
xi: reg upgrade sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", append se bdec(3)
xi: reg downgrade sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef.xls", append se bdec(3)

**************************************************
*Interactions of rating with industry (large cap)*
**************************************************

xi: reg default sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", replace se bdec(3)
xi: reg upgrade sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", append se bdec(3)
xi: reg downgrade sicdumrat* sicdum* lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", append se bdec(3)
xi: reg default sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", append se bdec(3)
xi: reg upgrade sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", append se bdec(3)
xi: reg downgrade sicdumrat* sicdum* retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large.xls", append se bdec(3)

**********************************************************
*Interactions of rating with finance industry (large cap)*
**********************************************************

gen findumrat=sicdum7*rating

xi: reg default findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", replace se bdec(3)
xi: reg upgrade findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", append se bdec(3)
xi: reg downgrade findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", append se bdec(3)
xi: reg default findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", append se bdec(3)
xi: reg upgrade findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", append se bdec(3)
xi: reg downgrade findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_finrest.xls", append se bdec(3)

xi: reg default findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", replace se bdec(3)
xi: reg upgrade findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", append se bdec(3)
xi: reg downgrade findumrat sicdum7 rating lnassets retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", append se bdec(3)
xi: reg default findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", append se bdec(3)
xi: reg upgrade findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", append se bdec(3)
xi: reg downgrade findumrat sicdum7 rating retann mktlev tang dd vol sp500 durann cash if lnassets>8.41, cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_large_finrest.xls", append se bdec(3)


forvalues i=1(1)10 {
gen sizesicdumrat`i'=sicdumrat`i'*sizedum
}

forvalues i=1(1)10 {
gen sizesicdum`i'=sicdum`i'*sizedum
}

gen sizerat=sizedum*rating

xi: reg default sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", replace se bdec(3)
xi: reg upgrade sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", append se bdec(3)
xi: reg downgrade sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* lnassets retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", append se bdec(3)
xi: reg default sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", append se bdec(3)
xi: reg upgrade sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", append se bdec(3)
xi: reg downgrade sizesicdumrat* sicdumrat* sizesicdum* sizerat sicdum* retann mktlev tang dd vol sp500 durann cash, nocons cluster(permno)
outreg2 using "/Users/mkacperc/Jeff/Results/New results 2016/table_findef_size.xls", append se bdec(3)


*******************************
*Generating yield break points*
*******************************

forvalues i=1(1)24 {
gen dum`i'=1 if rating>=`i'-1 & rating<`i'
replace dum`i'=0 if dum`i'==.
}

forvalues i=1(1)24 {
sum annyield if Frating~=. & dum`i'==1
}


******************
*Robustness Tests*
******************

*Regressions merger by merger
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==1
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==2
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==3
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==4
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==6
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==8
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==9
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==10
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==13
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==14
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==15
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==16
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==17
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 i.year if sum2~=. & merger==18

*Regressions for total coverage
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov<5,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, replace se bdec(3) coefastr
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov>=5&totcov2<20,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, append se bdec(3) coefastr
xi:reg Frating dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov>=20,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, append se bdec(3) coefastr
xi:reg Frating dum ST INT lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov<5,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, append se bdec(3) coefastr
xi:reg Frating dum ST INT lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov>=5&totcov2<20,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, append se bdec(3) coefastr
xi:reg Frating dum ST INT lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=. & totcov>=20,cluster(merger)
outreg2 using \Jeff\table_iv_nonlin22_totcov.txt, append se bdec(3) coefastr

*Adjustments of bond reports
xi:reg nba dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=.,cluster(merger)
outreg2 using \Jeff\table_iv_bonds_adjust.txt, replace se bdec(3) coefastr
xi:reg nrep dum stock inter lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=.,cluster(merger)
outreg2 using \Jeff\table_iv_bonds_adjust.txt, append se bdec(3) coefastr
xi:reg nba dum ST INT lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=.,cluster(merger)
outreg2 using \Jeff\table_iv_bonds_adjust.txt, append se bdec(3) coefastr
xi:reg nrep dum ST INT lnassets1-lnassets10 lnbmpv1-lnbmpv10 retann1-retann10 totcov1-totcov10 tang1-tang10 vol1-vol10 mktlev1-mktlev10 dd1-dd10 sp500 i.year i.merger if sum2~=.,cluster(merger)
outreg2 using \Jeff\table_iv_bonds_adjust.txt, append se bdec(3) coefastr



************************
*Results for DGTW match*
************************

use "/Users/mkacperc/Jeff/reginst.dta",clear
sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/DGTW_match.dta",nokeep
drop _merge

*sort permno year
*by permno: replace portfolio=portfolio[_n-1] if portfolio==. & portfolio[_n-1]~=.
*gsort permno -year
*by permno: replace portfolio=portfolio[_n-1] if portfolio==. & portfolio[_n-1]~=.

sort merger stock dum portfolio
by merger stock dum portfolio: egen ratingbench=mean(Frating)

sort merger stock dum portfolio
by merger stock dum portfolio: egen covbench=mean(noan)

sort merger stock dum portfolio
by merger stock dum portfolio: egen assetsbench=mean(lnassets)

sort merger stock dum portfolio
by merger stock dum portfolio: egen bmpvbench=mean(lnbmpv)

sort merger stock dum portfolio
by merger stock dum portfolio: egen mombench=mean(retann)

sort merger stock dum portfolio
by merger stock dum portfolio: egen levbench=mean(mktlev)

sort merger stock dum portfolio
by merger stock dum portfolio: egen ddbench=mean(dd)

sort merger stock dum portfolio
by merger stock dum portfolio: egen volbench=mean(vol)

save \Jeff\Data\temp.dta,replace

keep merger year ratingbench covbench assetsbench bmpvbench mombench levbench ddbench volbench portfolio stock
keep if stock==0

drop stock
duplicates drop

sort merger portfolio year
save \Jeff\Data\dgtw_control,replace

clear
use "/Users/mkacperc/Jeff/Data/temp.dta"
keep if stock==1
drop ratingbench covbench assetsbench bmpvbench mombench levbench ddbench volbench
sort merger portfolio year

merge merger portfolio year using "/Users/mkacperc/Jeff/Data/dgtw_control.dta",nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench
gen adjsize=lnassets-assetsbench
gen adjbm=lnbmpv-bmpvbench
gen adjmom=retann-mombench
gen adjlev=mktlev-levbench
gen adjdd=dd-ddbench
gen adjvol=vol-volbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

by merger permno: gen diffsize=adjsize-adjsize[_n-1]
by merger permno: gen diffbm=adjbm-adjbm[_n-1]
by merger permno: gen diffmom=adjmom-adjmom[_n-1]
by merger permno: gen difflev=adjlev-adjlev[_n-1]
by merger permno: gen diffdd=adjdd-adjdd[_n-1]
by merger permno: gen diffvol=adjvol-adjvol[_n-1]

*Table 7 (basic regression)
reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_basic.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_basic.txt, append se bdec(3) coefastr

*Table 7B (Nonlinear coverage)
reg diffrat if noan<5,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin.txt, replace se bdec(3) coefastr
reg diffrat if noan>=5&noan<20,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin.txt, append se bdec(3) coefastr
reg diffrat if noan>=20,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin.txt, append se bdec(3) coefastr

*Table 8 (placebo tests-controls)
keep if diffrat~=.
reg diffsize,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, replace se bdec(3) coefastr
reg diffbm,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, append se bdec(3) coefastr
reg diffmom,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, append se bdec(3) coefastr
reg difflev,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, append se bdec(3) coefastr
reg diffdd,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, append se bdec(3) coefastr
reg diffvol,cluster(permno)
outreg2 using \Jeff\table_placebo.txt, append se bdec(3) coefastr

***********************************
*Table 8B (non-overlapping stocks)*
***********************************

****DGTW match
use "/Users/mkacperc/Jeff/reginst.dta",clear
sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/DGTW_match.dta",nokeep
drop _merge

gen stock4=0
replace stock4=1 if (stock1==1 & stock2==0) | (stock1==0 & stock2==1)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen ratingbench=mean(Frating)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen covbench=mean(noan)

save "/Users/mkacperc/Jeff/Data/temp2.dta",replace

keep merger year ratingbench covbench portfolio stock4
keep if stock4==0

drop stock4
duplicates drop

sort merger portfolio year
save "/Users/mkacperc/Jeff/Data/dgtw_control2.dta",replace

clear
use "/Users/mkacperc/Jeff/Data/temp2.dta"
keep if stock4==1
drop ratingbench covbench
sort merger portfolio year

merge merger portfolio year using "/Users/mkacperc/Jeff/Data/dgtw_control2.dta",nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

*Table 8B (basic regression)
reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap.txt, append se bdec(3) coefastr


************************************************
*Table 8C (non-overlapping stocks as benchmark)*
************************************************

****DGTW match
use "/Users/mkacperc/Jeff/reginst.dta",clear
sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/DGTW_match.dta",nokeep
drop _merge

gen stock4=1 if (stock1==1 & stock2==0) | (stock1==0 & stock2==1)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen ratingbench=mean(Frating)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen covbench=mean(noan)

save "/Users/mkacperc/Jeff/Data/temp3.dta",replace

keep merger year ratingbench covbench portfolio stock4
keep if stock4==1

drop stock4
duplicates drop

sort merger portfolio year
save dgtw_control3,replace

clear
use "/Users/mkacperc/Jeff/Data/temp3.dta"
keep if stock==1
drop ratingbench covbench
sort merger portfolio year

merge merger portfolio year using "/Users/mkacperc/Jeff/Data/dgtw_control3.dta",nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

*Table 8C (basic regression)
reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap2.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap2.txt, append se bdec(3) coefastr


************************************
*Results for the revised instrument*
************************************

****DGTW match
use "/Users/mkacperc/Jeff/Data/reginst.dta",clear
sort permno year
merge permno year using "/Users/mkacperc/Jeff/Data/DGTW_match.dta",nokeep
drop _merge

sort permno year
by permno: replace portfolio=portfolio[_n-1] if portfolio==. & portfolio[_n-1]~=.
gsort permno -year
by permno: replace portfolio=portfolio[_n-1] if portfolio==. & portfolio[_n-1]~=.

sort merger ST dum portfolio
by merger ST dum portfolio: egen ratingbench=mean(Frating)

sort merger ST dum portfolio
by merger ST dum portfolio: egen covbench=mean(noan)

sort merger ST dum portfolio
by merger ST dum portfolio: egen assetsbench=mean(lnassets)

sort merger ST dum portfolio
by merger ST dum portfolio: egen bmpvbench=mean(lnbmpv)

sort merger ST dum portfolio
by merger ST dum portfolio: egen mombench=mean(retann)

sort merger ST dum portfolio
by merger ST dum portfolio: egen levbench=mean(mktlev)

sort merger ST dum portfolio
by merger ST dum portfolio: egen ddbench=mean(dd)

sort merger ST dum portfolio
by merger ST dum portfolio: egen volbench=mean(vol)

save \Jeff\Data\temp_rev.dta,replace

keep merger year ratingbench covbench assetsbench bmpvbench mombench levbench ddbench volbench portfolio ST
keep if ST==0

drop ST
duplicates drop

sort merger portfolio year
save "/Users/mkacperc/Jeff/Data/dgtw_control_rev.dta",replace

clear
use "/Users/mkacperc/Jeff/Data/temp_rev.dta"
keep if ST==1
drop ratingbench covbench assetsbench bmpvbench mombench levbench ddbench volbench
sort merger portfolio year

merge merger portfolio year using "/Users/mkacperc/Jeff/Data/dgtw_control_rev.dta",nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench
gen adjsize=lnassets-assetsbench
gen adjbm=lnbmpv-bmpvbench
gen adjmom=retann-mombench
gen adjlev=mktlev-levbench
gen adjdd=dd-ddbench
gen adjvol=vol-volbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

by merger permno: gen diffsize=adjsize-adjsize[_n-1]
by merger permno: gen diffbm=adjbm-adjbm[_n-1]
by merger permno: gen diffmom=adjmom-adjmom[_n-1]
by merger permno: gen difflev=adjlev-adjlev[_n-1]
by merger permno: gen diffdd=adjdd-adjdd[_n-1]
by merger permno: gen diffvol=adjvol-adjvol[_n-1]

*Table 7 (basic regression)
reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_basic_rev.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_basic_rev.txt, append se bdec(3) coefastr

*Table 7B (Nonlinear coverage)
reg diffrat if noan<5,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin_rev.txt, replace se bdec(3) coefastr
reg diffrat if noan>=5&noan<20,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin_rev.txt, append se bdec(3) coefastr
reg diffrat if noan>=20,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonlin_rev.txt, append se bdec(3) coefastr

*Table 8 (placebo tests-controls)
keep if diffrat~=.
reg diffsize,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, replace se bdec(3) coefastr
reg diffbm,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, append se bdec(3) coefastr
reg diffmom,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, append se bdec(3) coefastr
reg difflev,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, append se bdec(3) coefastr
reg diffdd,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, append se bdec(3) coefastr
reg diffvol,cluster(permno)
outreg2 using \Jeff\table_placebo_rev.txt, append se bdec(3) coefastr


***********************************
*Table 8B (non-overlapping stocks)*
***********************************

****DGTW match
use "/Users/mkacperc/Jeff/reginst.dta",clear
sort permno year
merge permno year using DGTW_match,nokeep
drop _merge

gen stock4=0
replace stock4=1 if (stock1==1 & stock2==0) | (stock1==0 & stock2==1)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen ratingbench=mean(Frating)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen covbench=mean(noan)

save temp2.dta,replace

keep merger year ratingbench covbench portfolio stock4
keep if stock4==0

drop stock4
duplicates drop

sort merger portfolio year
save dgtw_control2,replace

clear
use temp2
keep if stock4==1
drop ratingbench covbench
sort merger portfolio year

merge merger portfolio year using dgtw_control2,nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

*Table 8B (basic regression)
reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap.txt, append se bdec(3) coefastr

************************************************
*Table 8C (non-overlapping stocks as benchmark)*
************************************************

****DGTW match
use "/Users/mkacperc/Jeff/reginst.dta",clear
sort permno year
merge permno year using DGTW_match,nokeep
drop _merge

gen stock4=1 if (stock1==1 & stock2==0) | (stock1==0 & stock2==1)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen ratingbench=mean(Frating)

sort merger stock4 dum portfolio
by merger stock4 dum portfolio: egen covbench=mean(noan)

save temp3.dta,replace

keep merger year ratingbench covbench portfolio stock4
keep if stock4==1

drop stock4
duplicates drop

sort merger portfolio year
save dgtw_control3,replace

clear
use temp3
keep if stock==1
drop ratingbench covbench
sort merger portfolio year

merge merger portfolio year using dgtw_control3,nokeep
drop _merge

gen adjrating=Frating-ratingbench
gen adjcov=noan-covbench

sort merger permno year
by merger permno: gen diffrat=adjrating-adjrating[_n-1]
by merger permno: gen diffcov=adjcov-adjcov[_n-1]

*Table 8C (basic regression)*

reg diffcov if diffrat~=.,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap2.txt, replace se bdec(3) coefastr
reg diffrat,cluster(permno)
outreg2 using \Jeff\table_iv_nonpar_nonoverlap2.txt, append se bdec(3) coefastr


********************************
*Evaluating Investment Strategy*
********************************

*Selecting the pool of treated and control stocks

clear all
set mem 1g
use "/Users/mkacperc/Jeff/reginst.dta"

keep permno year  dum stock sic3 lnsize merger durann

gen sic2=int(sic3/10)
drop sic3

duplicates drop

sort permno year
save "/Users/mkacperc/Jeff/Data/affected.dta",replace

clear all
set mem 4g
use "/Users/mkacperc/Jeff/Data/bonds yield return.dta"

replace cusip = subinstr(cusip, "'", "",.) 

sort cusip year
merge cusip year using "/Users/mkacperc/Jeff/Data/cusip_permno.dta",nokeep
tab _merge

drop _merge cusip2

sort cusip year month
by cusip: replace permno=permno[_n-1] if permno==. & permno[_n-1]~=.

sort permno year month


drop if yield<=0

sort permno year
merge permno year using "\Jeff\Data\affected.dta",nokeep
tab _merge

drop _merge

gen estdats=year*100+month

*Defining the event window
gen event=1 if estdats==199501
replace event=1 if estdats==198410
replace event=1 if estdats==198811
replace event=1 if estdats==199706
replace event=1 if estdats==199712
replace event=1 if estdats==199801
replace event=1 if estdats==199803
replace event=1 if estdats==199804
replace event=1 if estdats==199910
replace event=1 if estdats==200007
replace event=1 if estdats==200011
replace event=1 if estdats==200012
replace event=1 if estdats==200101
replace event=1 if estdats==200110
replace event=1 if estdats==200504


gsort permno -estdats

by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]+1 if event==. & event[_n+1]~=.

sort permno estdats

by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.
by permno: replace event=event[_n+1]-1 if event==. & event[_n+1]~=.


tabstat yield if stock==1,by(event)
tabstat yield if stock==0,by(event)
tabstat return if stock==0,by(event)
tabstat return if stock==1,by(event)


gen E=0 if event>-3 & event<=0
replace E=1 if event>0 & event <=3

reg  yield E if stock==1, cluster(permno)
xi: areg  yield E if stock==1,absorb(sic2) cluster(permno)
xi: areg  yield E if stock==1,absorb(permno) cluster(permno)
xi: areg  yield E i.sic2 if stock==1,absorb(permno) cluster(permno)

*Analysis of returns post event
gen E2=0 if event>=1 & event<=3
replace E2=1 if event>=4 & event<=6
xi: areg return E2 i.sic2 if stock==1,absorb(permno) cluster(permno)
xi: areg return E2 i.sic2 if stock==1,absorb(year) cluster(permno)

gen control=.

sort event merger durann
by event merger: replace control=1 if stock==0 & stock[_n+1]==1 & event==0

save "/Users/mkacperc/Jeff/Data/returns_analysis.dta",replace

clear all
use "/Users/mkacperc/Jeff/Data/returns_analysis.dta"

sort merger permno event
by merger permno: replace control=control[_n-1] if control[_n-1]==1 & control==.

gsort merger permno -event
by merger permno: replace control=control[_n-1] if control[_n-1]==1 & control==.

replace control=0 if control==. & stock==1

tabstat yield if stock==1 & merger~=.,by(event)
tabstat yield if control==1 & merger~=.,by(event)
tabstat return if control==1,by(event)
tabstat return if stock==1,by(event)

gen E3=0 if event>=1 & event<=3
replace E3=1 if event>=4 & event<=12
gen inter3=control*E3

*Calculating average returns of the long-short strategy
reg return E3 inter3 control,cluster(year)
xi: areg return E3 inter3 control,absorb(permno) cluster(year)
xi: areg return E3 inter3 control durann lnsize,absorb(permno) cluster(year)
xi: areg return E3 inter3 control durann lnsize,absorb(sic2) cluster(year)

xi: areg yield E3 inter3 control durann lnsize,absorb(permno) cluster(year)

*Regressions for treatment-control
areg return control durann lnsize i.year if event>=1 & event<=3,absorb(sic2) cluster(year)
outreg2 using \Jeff\table_returns.txt, replace se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=6,absorb(sic2) cluster(year)
outreg2 using \Jeff\table_returns.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=9,absorb(sic2) cluster(year)
outreg2 using \Jeff\table_returns.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=12,absorb(sic2) cluster(year)
outreg2 using \Jeff\table_returns.txt, append se bdec(3) coefastr

areg return control durann lnsize i.year if event>=1 & event<=1,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, replace se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=2,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=3,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=6,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=9,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, append se bdec(3) coefastr
areg return control durann lnsize i.year if event>=1 & event<=12,absorb(sic2)
outreg2 using \Jeff\table_returns2.txt, append se bdec(3) coefastr

*Sequence of returns by event period (ending in period t-1)
areg return control durann lnsize i.year if event>=3 & event<=3,absorb(sic2)
areg return control durann lnsize i.year if event>=4 & event<=4,absorb(sic2)
areg return control durann lnsize i.year if event>=5 & event<=5,absorb(sic2)
areg return control durann lnsize i.year if event>=6 & event<=6,absorb(sic2)
areg return control durann lnsize i.year if event>=7 & event<=7,absorb(sic2)
areg return control durann lnsize i.year if event>=8 & event<=8,absorb(sic2)
areg return control durann lnsize i.year if event>=9 & event<=9,absorb(sic2)
areg return control durann lnsize i.year if event>=10 & event<=10,absorb(sic2)
areg return control durann lnsize i.year if event>=11 & event<=11,absorb(sic2)
areg return control durann lnsize i.year if event>=12 & event<=12,absorb(sic2)
areg return control durann lnsize i.year if event>=13 & event<=13,absorb(sic2)

*Generating cumulative returns by quarter
gen q=1 if event>=3 & event<=5
replace q=2 if event>=6 & event<=8
replace q=3 if event>=9 & event<=11
replace q=4 if event>=12 & event<=14

sort permno year q
gen Ret=1+return

keep if event>2 & event<14

replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.
replace Eve=Eve[_n-1]+1 if event-event[_n-1]~=1&Eve[_n-1]~=.
replace Eve=Eve[_n-1] if event-event[_n-1]==1 & Eve[_n-1]~=.

sort Eve

by Eve: egen totret12=sum(ret) if q==1
by Eve: egen totret22=sum(ret) if q>=1 & q<=2
by Eve: egen totret32=sum(ret) if q>=1 & q<=3
by Eve: egen totret42=sum(ret) if q>=1 & q<=4

areg totret1 control durann lnsize  if event==3 | event==4 | event==5,absorb(sic2)
areg totret2 control durann lnsize  if event==6 | event==7 | event==8,absorb(sic2)
areg totret3 control durann lnsize  if event==9 | event==10 | event==11,absorb(sic2)
areg totret4 control durann lnsize  if event==12 | event==13,absorb(sic2)

areg totret4 control durann lnsize  if event==5,absorb(sic2) cluster(year)
areg totret3 control durann lnsize  if event==5,absorb(sic2) cluster(year)
areg totret2 control durann lnsize  if event==5,absorb(sic2) cluster(year)
areg totret1 control durann lnsize  if event==5,absorb(sic2) cluster(year)

*Final results
xi: areg totret12 control durann lnsize if N<=11 & event==4,absorb(sic2)
xi: areg totret22 control durann lnsize if N<=11 & event==4,absorb(sic2)
xi: areg totret32 control durann lnsize if N<=11 & event==5,absorb(sic2)
xi: areg totret42 control durann lnsize if N<=11 & event==4,absorb(sic2)

save "/Users/mkacperc/Jeff/returns_strat.dta",replace
